package com.tsu.mall.order.Controller;

import com.tsu.mall.common.entity.Bill;
import com.tsu.mall.common.entity.Member;
import com.tsu.mall.common.service.IBillService;
import com.tsu.mall.common.service.IMemberService;
import com.tsu.mall.common.util.RequestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/myBill")
public class MyBillController {
    @Autowired
    private IMemberService iMemberService;
    @Autowired
    private IBillService iBillService;
    @GetMapping("/myBillPage")
    public String myBilPage(Model model){
        //查询当前登录用户信息
        Member currentMember=iMemberService.doSelectById(RequestUtil.getCurrent().getId());
        model.addAttribute("currentMember",currentMember);
        return "myBill";
    }
    @PostMapping("/billInfo")
    @ResponseBody
    public List<Bill> billInfo(){
        //查询订单表里当前会员的所有订单信息
        return iBillService.doList(RequestUtil.getCurrent().getId());
    }

    @PostMapping("/selectByStatus")
    @ResponseBody
    public List<Bill> selectByStatus(String status){
        //根据状态查询订单表里当前会员的所有订单信息
        return iBillService.doListByStatus(RequestUtil.getCurrent().getId(),status);
    }

    //监听队列 接收消息
    @JmsListener(destination = "bill.save")
    public void billCheck(Integer id){
        //根据id查询订单
        Bill bill=iBillService.doSelectByid(id);
        //判断是否还是待支付
        if(bill.getStatus().equals("待支付")){
            iBillService.updateStatusById(id);//如果还是 改为已取消
        }
    }


}
